//index.js
//获取应用实例
const app = getApp()
let timeId = null;
Page({
  data: {
    imgUrls: [{
      titleImg: "https://img.zhichiwangluo.com/zcimgdir/thumb/t_15560188865cbef6c6bc83a.jpg",
      openpath: ""
    }, {
        titleImg: "https://img.zhichiwangluo.com/zcimgdir/thumb/t_15560188075cbef6773d889.jpg",
      openpath: ""
    },{
        titleImg: "https://img.zhichiwangluo.com/zcimgdir/thumb/t_15560188385cbef69690c46.jpg",
        openpath:""
    }],
    server: [{
      img: "http://img.weiye.me/zcimgdir/album/file_58ca5fa616b3b.png",
        openpath: "../../pages/caseVideo/caseVideo",
        text: "心理测评"
      },
      {
        img: "http://img.weiye.me/zcimgdir/album/file_58ca5fa59568e.png",
        openpath: "/pages/consult/consult",
        text: "咨询预约"
      },
      {
        img: "http://img.weiye.me/zcimgdir/album/file_58ca5fa594847.png",
        openpath: "../../pages/coursewareCenter/coursewareCenter",
        text: "心吧客"
      },
      {
        img: "http://img.weiye.me/zcimgdir/album/file_58ca5fa59b9ed.png",
        openpath: "../../pages/2048/2048",
        text: "能源站"
      }
    ],
    audio: {
      // 歌手封面
      poster: "https://y.gtimg.cn/music/photo_new/T002R300x300M000000VftF52ustnt.jpg?max_age=2592000",
      // 歌曲名称
      name: "Set Fire to the Rain",
      // 演唱
      author: "Adele",
      // 歌曲链接
      src: "http://isure.stream.qqmusic.qq.com/C400002GRV0U2D7k8J.m4a?guid=1080882400&vkey=5A46ED71644526449BE96FDAEBA684DE434A53CE2225F6AF566E653E283EE53F19BCBA2A34A33A2F5AB443CC2BF033A5C04F67C48D334BBD&uin=1938&fromtag=66"
    },
    isHide: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    innerAudio: undefined,
    newsData: [],
    showKeywords:false,
    keywords: ['讨好型人格', '人际关系', '友谊', '恋爱']
  },
  showInput: function() {
    this.setData({
      inputShowed: true
    });
  },
  clearInput: function() {
    this.setData({
      inputVal: "",
      showKeywords:false
    });
  },
  inputTyping: function(e) {
    if(!e.detail.value){
      this.setData({
        showKeywords:false
      })
    }else{
      if(!this.data.showKeywords){
        //timeId && clearTimeout(timeId);
        timeId = setTimeout(() =>{
          this.setData({
            showKeywords:true
          })
        },1000)
      }
    }
    this.setData({
      inputVal: e.detail.value
    });
  },
  keywordHandle(e){
    const text = e.target.dataset.text;
    console.log(text);
    this.setData({
      inputVal: text,
      showKeywords: false,
    })
  },
  searchArticle(){
    var that = this;
    const val = this.data.inputVal;

    wx.request({
      url: "http://192.168.50.85:8080/psychology/search/" + val,
      method:"GET",
      success:function(res){
        if (res.data && res.data.data && res.data.data.length > 0) {
          for (var newsItem of res.data.data) {
            var labelArray = newsItem.labels.split(",");
            var newLabel = '';
            for (var val of labelArray) {
              var a = " #" + val;
              newLabel += a;
            }
            newsItem.labels = newLabel;
          }
          that.setData({
            newsData: res.data.data
          })
        }
      }
    })
  },
  onLoad: function() {
    var that = this;
    // 获取用户当前设置，查看是否已经授权
    wx.getSetting({
      success: res => {
        // 用户已经授权，则获取用户信息
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function(res) {
              app.globalData.userInfo = res.userInfo
            }
          })
        } else {
          // 用户没有授权，改变isHide的值
          that.setData({
            isHide: true
          })
        }
      }
    });

    wx.request({
      url: 'http://192.168.50.85:8080/psychology',
      method:"GET",
      success : function(res){
        if(res.data && res.data.data && res.data.data.length > 0){
          for(var newsItem of res.data.data){
            var labelArray = newsItem.labels.split(",");
            var newLabel = '';
            for(var val of labelArray){
              var a = " #" + val;
              newLabel += a;
            }
              newsItem.labels = newLabel;
          }
          that.setData({
            newsData:res.data.data
          })
        }
      }
    })
  },

  bindGetUserInfo: function(e) {
    var that = this;
    //用户允许授权
    if (e.detail.userInfo) {
      var that = this;
      app.globalData.userInfo = e.detail.userInfo
      // 授权成功之后，改变isHide的值，让主页面显示，隐藏授权页面
      that.setData({
        isHide: false
      })
      // 授权成功之后，调用wx.login,获取code传给后台
      wx.getUserInfo({
        success: function(res) {
          var userNick = res.userInfo.nickName;
          var avataUrl = res.userInfo.avatarUrl;
          var gender = res.userInfo.gender;
          wx.login({
            success: function(res) {
              var code = res.code

              wx.request({
                url: 'http://localhost:8080/user',
                method: "POST",
                data: {
                  code: code,
                  nick: userNick,
                  imgUrl: avataUrl,
                  gender: gender
                },
                success: function(res) {
                  // app.globalData.userId = res.data.data.id
                  // app.globalData.userRole = res.data.data.role
                  app.globalData.userInfo = res.data.data
                  app.globalData.canUserEnable = app.globalData.userInfo.status
                  that.setData({
                    canUserEnable: app.globalData.canUserEnable
                  })
                }
              })
            }
          })
        }
      })
    } else { // 用户拒绝授权
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入',
        showCancel: false,
        confirmText: '返回授权'
      })
    }
  }
})